﻿using CefSharp;
using CefSharp.WinForms;
using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace jihuang_Player
{
    public partial class newProject : Form
    {
        ChromiumWebBrowser chromiumWebBrowser;
        string htmlPath;
        MainWindow mainWindow;
        public newProject(MainWindow window)
        {
            mainWindow = window;
            string rootPath = System.IO.Directory.GetCurrentDirectory();
            htmlPath = @"file:///" + System.Web.HttpUtility.UrlEncode(rootPath.Replace(@"\", @"/")).Replace("%3a", ":").Replace("%2f", "/") + "/newProject.html"; ;
            chromiumWebBrowser = new ChromiumWebBrowser(htmlPath);
            InitializeComponent();
            if (!MainWindow.debugmode)
            {
                button1.Hide();
                button2.Hide();
            }
            this.Controls.Add(chromiumWebBrowser);
            chromiumWebBrowser.JavascriptObjectRepository.Register("jsMethod2", new jsMethod2(this), false);
        }

        private void newProject_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            chromiumWebBrowser.Load(htmlPath);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            chromiumWebBrowser.ShowDevTools();
        }
        public void complate()
        {
            mainWindow.notifyreflushProjetlist();
            MainWindow.flag = true;
        }
    }
    public class jsMethod2
    {
        newProject window;
        public jsMethod2(newProject w)
        {
            window = w;
        }

        private void CopyDirectory(string sourceDirPath, string SaveDirPath)
        {
            try
            {
                //如果指定的存储路径不存在，则创建该存储路径
                if (!Directory.Exists(SaveDirPath))
                {
                    //创建
                    Directory.CreateDirectory(SaveDirPath);
                }
                //获取源路径文件的名称
                string[] files = Directory.GetFiles(sourceDirPath);
                //遍历子文件夹的所有文件
                foreach (string file in files)
                {
                    string pFilePath = SaveDirPath + "\\" + Path.GetFileName(file);
                    if (File.Exists(pFilePath))
                        continue;
                    File.Copy(file, pFilePath, true);
                }
                string[] dirs = Directory.GetDirectories(sourceDirPath);
                //递归，遍历文件夹
                foreach (string dir in dirs)
                {
                    CopyDirectory(dir, SaveDirPath + "\\" + Path.GetFileName(dir));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public void complet(string file, string name)
        {
            Console.WriteLine(file);
            Console.WriteLine(name);
            string path = MainWindow.rootPath + @"\Project\" + name + @"\";
            Console.WriteLine(path);
            if (Directory.Exists(path))
            {
                MessageBox.Show("工程：\"" + name + "\" 已存在，请输入其他名称");
            }
            else
            {
                CopyDirectory(MainWindow.rootPath + @"\resources\publicProjectSources\", MainWindow.rootPath + @"\Project\" + name);
                CopyDirectory(MainWindow.rootPath + @"\resources\模板\" + file, MainWindow.rootPath + @"\Project\" + name);
                window.complate();
            }
        }
    }
}
